package com.yun.client;



import com.yun.config.RestTemplateConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

@Component
public class Httpclient {
    private final RestTemplate template;
    public Httpclient() {
        this.template = new AnnotationConfigApplicationContext(RestTemplateConfig.class).getBean(RestTemplate.class);
    }
    public String client(String url){
        ResponseEntity<String> response = new ResponseEntity<String>(HttpStatus.OK);
        try{
            response= template.getForEntity(url,String.class);
            System.out.println("获取的信息:\n"+response.getBody());
        }catch (RestClientException e){
            if (e.getCause() instanceof java.net.SocketTimeoutException) {
                return "请求超时";
            }
        }
        return response.getBody();
    }
}
